<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
		<title>字典管理</title>
		[#include "/html/include/easyuicss.html" /]
		[#include "/html/include/easyuijs.html" /]
	</head>
	<body class="easyui-layout" data-options="fit:true">
		<form id="roleForm" class="easyui-form" method="post" data-options="novalidate:true">
			<input type="hidden" name="method" value="${RequestParameters['method']}" />
			<input type="hidden" id="items" name="items" />
			<table class="tblEidt">
				<tr>
					<td class="tdText">
						字典组编码<label style="color:red;">*</label>：
					</td>
					<td>
						[#if RequestParameters['method']=='create']
						<input class="easyui-textbox" name="groupId" value="${group.group_id}" data-options="required:true,validateOnCreate:false,validType:{remote:['${app}/sysmgr/dict/checkGroupId.do','groupId']}" />
						[#else]
						<input class="easyui-textbox" name="groupId" value="${group.group_id}" readonly />
						[/#if]
					</td>
					<td class="tdText">
						字典组名称<label style="color:red;">*</label>：
					</td>
					<td class="tdInput">
						<input class="easyui-textbox" name="groupName" value="${group.group_name}" data-options="required:true" />
					</td>
				</tr>
				<tr>
					<td class="tdText">
						字典描述<label style="color:red;">*</label>：
					</td>
					<td class="tdInput" colspan="3">
						<input class="easyui-textbox" name="groupDesc" value="${group.group_desc}" style="width:80%;height:60px;" data-options="multiline:true,required:true" />
					</td>
				</tr>
			</table>
			<table id="itemdg" class="easyui-datagrid" title="字典项信息" style="width:100%;height:250px;"
				   data-options="
						fitColumns: true,
						singleSelect: true,
						toolbar: '#tb',
						onClickRow: onClickRow
					">
				<thead>
				<tr>
					<th data-options="field:'item_id',width:200,editor:{type:'textbox',options:{required: true}}">字典项编码</th>
					<th data-options="field:'item_name',width:180,editor:{type:'textbox',options:{required: true}}">字典项名称</th>
				</tr>
				</thead>
			</table>
			<div class="edit-btn-group">
				<a href="javascript:;" class="easyui-linkbutton"
				   data-options="iconCls:'icon-ok'" style="width:80px"
				   onclick="saveDict(this);">保存</a>
				&nbsp;&nbsp;
				<a href="javascript:;" class="easyui-linkbutton"
				   data-options="iconCls:'icon-cancel'" style="width:80px"
				   onclick="closeWindow();">关闭</a>
			</div>
			<div id="tb" style="height:auto">
				<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="append()">添加</a>
				<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="removeit()">删除</a>
			</div>
		</form>
	</body>
    <script type="text/javascript">
		var index = parent.layer.getFrameIndex(window.name);
		var editIndex = undefined;
		function closeWindow(){
			parent.layer.close(index);
		}
		$.parser.onComplete = function() {
			$('#itemdg').datagrid('loadData',{rows:jQuery.parseJSON('${items}')});
		}
		function saveDict(obj){
			$(obj).linkbutton('disable');
			$('#roleForm').form('submit', {
				url: '${app}/sysmgr/dict/save.do',
				onSubmit: function () {
					if (endEditing()){
						var rows = $('#itemdg').datagrid('getRows');
						//alert(JSON.stringify(rows));
						/*for (var i=0;i<rows.length;i++){
							alert(JSON.stringify(rows[i]));
						}*/
						var itemExists = false;
						$(rows).each(function(i){
							var iv = rows[i].item_id;
							$(rows).each(function(j){
								var jv = rows[j].item_id;
								if(i != j && iv==jv){
									itemExists = true;
								}
							});
						});
						if(itemExists){
							easyui.alert('字典项编码有重复！', '系统提醒');
							$(obj).linkbutton('enable');
							return false;
						}
						$('#items').val(JSON.stringify(rows));
					}

					var valid = $(this).form('enableValidation').form('validate');
					if (!valid) {
						$(obj).linkbutton('enable');
					}
					return valid;
				},
				success: function (data) {
					var result = $.parseJSON(data);
					parent.easyui.msgShow('系统提醒', result.msg, 3000, 250, 100);
					$(obj).linkbutton('enable');
					if (result.result == 1) {
						parent.layer.close(index);
						parent.frames[parent.iframeName].window.reloadDict();
					}
				}
			});
		}
		function endEditing(){
			if (editIndex == undefined){return true}
			if ($('#itemdg').datagrid('validateRow', editIndex)){
				var ed = $('#itemdg').datagrid('getEditor', {index:editIndex,field:'item_id'});
				var itemId = $(ed.target).val();
				$('#itemdg').datagrid('getRows')[editIndex]['item_id'] = itemId;
				$('#itemdg').datagrid('endEdit', editIndex);
				editIndex = undefined;
				return true;
			} else {
				return false;
			}
		}
		function onClickRow(index){
			if (editIndex != index){
				if (endEditing()){
					$('#itemdg').datagrid('selectRow', index)
							.datagrid('beginEdit', index);
					editIndex = index;
				} else {
					$('#itemdg').datagrid('selectRow', editIndex);
				}
			}
		}
		function append(){
			if (endEditing()){
				$('#itemdg').datagrid('appendRow', {});
				editIndex = $('#itemdg').datagrid('getRows').length-1;
				$('#itemdg').datagrid('selectRow', editIndex)
						.datagrid('beginEdit', editIndex);
			}
		}
		function removeit(){
			if (editIndex == undefined){return}
			$('#itemdg').datagrid('cancelEdit', editIndex)
					.datagrid('deleteRow', editIndex);
			editIndex = undefined;
		}
    </script>
</html>